package com.auxgroup.hr.front.site.co.modPerson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.modperson.PersonProcessRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCAppStepInst;
import com.auxgroup.hr.front.dao.domain.PsCModBankacct;
import com.auxgroup.hr.front.dao.domain.PsCModEducat;
import com.auxgroup.hr.front.dao.domain.PsCModEmpLang;
import com.auxgroup.hr.front.dao.domain.PsCModEmpPhoto;
import com.auxgroup.hr.front.dao.domain.PsCModFamily;
import com.auxgroup.hr.front.dao.domain.PsCModItTech;
import com.auxgroup.hr.front.dao.domain.PsCModJcxx;
import com.auxgroup.hr.front.dao.domain.PsCModPersData;
import com.auxgroup.hr.front.dao.domain.PsCModPhotoTactPerPrimary;
import com.auxgroup.hr.front.dao.domain.PsCModSocial;
import com.auxgroup.hr.front.dao.domain.PsCModTactPer;
import com.auxgroup.hr.front.dao.domain.PsCModTrain;
import com.auxgroup.hr.front.dao.domain.PsCModWorkHi;
import com.auxgroup.hr.front.dao.domain.PsCModZcgl;
import com.auxgroup.hr.front.dao.domain.PsCWfModHdr;
import com.auxgroup.hr.front.dao.repository.PsCWfModHdrRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.modperson.ModBankacctService;
import com.auxgroup.hr.front.service.modperson.ModEducatService;
import com.auxgroup.hr.front.service.modperson.ModEmpLangService;
import com.auxgroup.hr.front.service.modperson.ModEmpPhotoService;
import com.auxgroup.hr.front.service.modperson.ModFamilyService;
import com.auxgroup.hr.front.service.modperson.ModItTechService;
import com.auxgroup.hr.front.service.modperson.ModJcxxService;
import com.auxgroup.hr.front.service.modperson.ModPersService;
import com.auxgroup.hr.front.service.modperson.ModSocialService;
import com.auxgroup.hr.front.service.modperson.ModTactPerService;
import com.auxgroup.hr.front.service.modperson.ModTrainService;
import com.auxgroup.hr.front.service.modperson.ModWorkHiService;
import com.auxgroup.hr.front.service.modperson.ModZcglService;
import com.auxgroup.hr.front.service.process.ProcessService;
import com.auxgroup.hr.front.service.process.WfFileService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.modperson.PersonProcessRespVO;
import com.auxgroup.hr.front.ws.ProcessApplyQueDTO;

/**
 * 个人信息修改CO
 * @author lvliuhao
 * @version 创建时间：180526 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/modPers")
public class ModPersCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(ModPersCO.class);

	@Autowired
	private ModPersService modPersService;
	
	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	@Autowired
	private ProcessService processService;


	/**
	 * 个人信息的增删改
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(PsCModPersData modPers,HttpServletRequest request) {
		logger.info("saveModPers param= {}", modPers);
		if(StringUtils.isBlank(modPers.getcTransactionNbr())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "流程编号不能为空");
		}
		if(StringUtils.isBlank(modPers.getEmplid())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "员工ID不能为空");
		}
		if(StringUtils.isBlank(modPers.getcModStatus())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "数据状态不能为空");
		}
		PsCModPersData nowPer = modPersService.getModPers(modPers.getcTransactionNbr());
		if(nowPer!=null){
			objectMapper.map(modPers, nowPer,"notCopyNull_modPers");
			modPers = modPersService.save(nowPer);
		}else{
			modPers = modPersService.save(modPers);
		}
		if(modPers.getEmplid()!=null){
			return buildRespone(true);
		}
		return buildRespone(false);
	}
	

	/**
	 * 个人信息流程提交
	 * @param modPers
	 * @param request
	 * @return
	 */
	@PostMapping(value="/processSubmit")
	public BaseResponseVO processSubmit(PsCModPersData modPers,HttpServletRequest request){
		logger.info("processSubmit param= {}", modPers);
		//先获取流程编号
		if(StringUtils.isBlank(modPers.getcTransactionNbr())){
			modPers.setcTransactionNbr(processService.getTranNum());
		}
		modPers = modPersService.save(modPers);
		ProcessApplyQueDTO requestDTO = new ProcessApplyQueDTO();
		requestDTO.setC_ACTION_TYPE("Submit");
		requestDTO.setC_TRANSACTION_NBR(modPers.getcTransactionNbr());
		requestDTO.setC_USERID(modPers.getEmplid());
		requestDTO.setC_WF_TYPE("AAA");
		Map<String, String> isSuccess = processService.apply(requestDTO);
		if(!isSuccess.containsKey("C_MSG_CODE")){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_APPLY_FAIL);
		}
		if("0".equals(isSuccess.get("C_MSG_CODE"))){
			return buildRespone();
		}
		return buildRespone(isSuccess.get("C_MSG_CODE"),isSuccess.get("C_MSG_TEXT"));
	}
	
}
